using System.Collections.Generic;
using UnityEngine;

public static class BezierMeshCalculator
{
	public static Vector3 PointOnPath(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
	{
		float num = 1f - t;
		float num2 = num * num;
		float d = num2 * num;
		float num3 = t * t;
		float d2 = num3 * t;
		Vector3 a = d * p0;
		a += 3f * num2 * t * p1;
		a += 3f * num * num3 * p2;
		return a + d2 * p3;
	}

	public static void CreateBezier(Vector3 start, Vector3 end, Vector3 handle1, Vector3 handle2, out List<Vector3> vertList, out List<int> triList, int resolution = 12, float thickness = 0.25f)
	{
		CreateBezierFromTo(start, end, handle1, handle2, out vertList, out triList, resolution, thickness);
	}

	public static void CreateBezierFromTo(Vector3 start, Vector3 end, Vector3 handle1, Vector3 handle2, out List<Vector3> vertList, out List<int> triList, int resolution = 12, float thickness = 0.25f, float from = 0f, float to = 1f)
	{
		float num = 1f;
		float num2 = thickness / 2f;
		vertList = new List<Vector3>();
		triList = new List<int>();
		Vector3 vector = new Vector3(0f, 0f, -1f);
		float num3 = to - from;
		float num4 = num3 / (float)resolution;
		for (int i = 0; i < resolution; i++)
		{
			float t = (float)i * num4 + from;
			float t2 = (float)(i + 1) * num4 + from;
			Vector3 vector2 = PointOnPath(t, start, handle1, handle2, end);
			Vector3 a = PointOnPath(t2, start, handle1, handle2, end);
			Vector3 rhs = a - vector2;
			if (i == 0 || i == resolution - 1)
			{
				rhs = new Vector3(0f, 1f, 0f);
			}
			rhs.Normalize();
			Vector3 a2 = Vector3.Cross(vector, rhs);
			a2 *= num2;
			Vector3 b = a2 + vector * (0f - num2);
			Vector3 b2 = -a2 + vector * (0f - num2);
			int count = vertList.Count;
			Vector3[] collection = new Vector3[2]
			{
				vector2 + b,
				vector2 + b2
			};
			vertList.AddRange(collection);
			int[] collection2 = new int[6]
			{
				count,
				count + 1,
				count + 2,
				count + 2,
				count + 1,
				count + 3
			};
			triList.AddRange(collection2);
			if (i == resolution - 1)
			{
				count = vertList.Count;
				vertList.AddRange(new Vector3[2]
				{
					a + b,
					a + b2
				});
			}
		}
	}
}
